// my_order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active_: 0,
    all:[
      {
        goods:[
          {
            url:'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price:'280',
            status:'待付款',
            num: '1'            
          },
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待付款',
            num: '1'
            
          }
        ],
        total:0,
        freight:'15.00'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待发货',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.00'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '已发货',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.23'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待发货',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.00'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '已发货',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.23'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待发货',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.00'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '已发货',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.23'
      }
    ],
    wait_pay:[
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待付款',
            num: '1'
          },
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待付款',
            num: '1'

          }
        ],
        total: 0,
        freight: '15.00'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待付款',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.00'
      },
      {
        goods: [
          {
            url: 'http://img5.imgtn.bdimg.com/it/u=779031813,2515846790&fm=26&gp=0.jpg',
            name: '法国·AOP  雕花干红葡萄酒 750ml',
            price: '280',
            status: '待付款',
            num: '1'
          }
        ],
        total: 0,
        freight: '15.23'
      }
    ]




  },
  switch_bar(e){
    let id = e.target.dataset.id
    this.setData({
      active_: id
    })
  },

  computed_total(name){
    let all = this.data[name]
    all.forEach(value => {
      value.goods.forEach(value1 => {
        value.total += parseFloat(value1.num) * parseFloat(value1.price)
      })
      value.total += parseFloat(value.freight)
    })
    this.setData({
      [name]: all
    })
  },
  getData(){
    let baseUrl = getApp().globalData.baseUrl,
        access_token = getApp().globalData.access_token,
        self = this
    wx.request({
      url: baseUrl + '/MyOrder/order',
      data: { access_token},
      method: 'GET',
      success: data => {
        self.setData({
          data: data.data.data
        })
        console.log(data.data)
      }
    })
  },
  // 立即付款
  payNow(e){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let order_no = e.currentTarget.dataset.id,
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this
      wx.request({
        url: baseUrl + '/Order/pay_info',
        method: 'POST',
        header,
        data: { access_token, order_no},
        success: data => {
          wx.hideLoading()
          if (parseInt(data.data.status) == 1){
            wx.requestPayment({
              timeStamp: data.data.timeStamp,
              nonceStr: data.data.nonceStr,
              package: data.data.package,
              signType: data.data.signType,
              paySign: data.data.paySign,
              success: data => {
                console.log(data)
                if (data.errMsg == 'requestPayment:ok'){
                  wx.showToast({
                    title: '支付成功！',
                    mask:true
                  })
                  setTimeout(function(){
                    self.getData()
                  },1000)
                }
              },
              fail: res => {
                if (res.errMsg == 'requestPayment:fail') {
                  wx.showToast({
                    title: '支付失败',
                    image: '../../image/error.png'
                  })
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '../my_order/my_order',
                    })
                  }, 1000)
                }
              }
            })
          }
        }
      })
  },
  // 取消订单
  cancel_order(e){
    console.log(e.currentTarget.dataset.id)
    wx.showModal({
      title: '提醒',
      content: '确认取消？',
      confirmColor: '#b21919',
      success: res => {
        if (res.confirm){
          wx.showLoading({
            title: '正在取消...',
            mask: true
          })
          let baseUrl = getApp().globalData.baseUrl,
            access_token = getApp().globalData.access_token,
            self = this,
            header = getApp().globalData.header,
            order_id = e.currentTarget.dataset.id
            wx.request({
              url: baseUrl + '/Order/cancel',
              method: 'POST',
              data: { access_token, order_id},
              header,
              success: data => {
                wx.hideLoading()
                if(data.data.code == 200){
                  wx.showToast({
                    title: '取消成功！',
                  })
                  setTimeout(function(){
                  self.getData()
                  },1000)
                }
              }
            })
        }else{
          return
        }
      }
    })
  },
  // 删除订单
  delete_order(e) {
    console.log(e.currentTarget.dataset.id)
    wx.showModal({
      title: '提醒',
      content: '确认删除？',
      confirmColor: '#b21919',
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            title: '正在删除...',
            mask: true
          })
          let baseUrl = getApp().globalData.baseUrl,
            access_token = getApp().globalData.access_token,
            self = this,
            header = getApp().globalData.header,
            order_id = e.currentTarget.dataset.id
          wx.request({
            url: baseUrl + '/MyOrder/order_delete',
            method: 'POST',
            data: { access_token, order_id },
            header,
            success: data => {
              wx.hideLoading()
              if (data.data.code == 200) {
                wx.showToast({
                  title: '删除成功！',
                })
                setTimeout(function () {
                  self.getData()
                }, 1000)
              }
            }
          })
        } else {
          return
        }
      }
    })
  },
  // 提醒发货
  remind(e){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let order_id = e.currentTarget.dataset.id,
    self = this,
    baseUrl = getApp().globalData.baseUrl,
    access_token = getApp().globalData.access_token,
    header = getApp().globalData.header
    wx.request({
      url: baseUrl + '/MyOrder/reminder',
      method: 'POST',
      header,
      data: { access_token, order_id},
      success: data => {
        wx.hideLoading()
        if(data.data.code == 200){
          wx.showToast({
            title: data.data.message,
          })
        }
      }
    })
    
  },
  // 查看物流
  check(e){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let order_id = e.currentTarget.dataset.id,
      self = this,
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      freight_num = e.currentTarget.dataset.freight_num
    wx.request({
      url: baseUrl + '/MyOrder/showFreight',
      data: { access_token, freight_num},
      method: 'POST',
      header,
      success: data => {
        wx.hideLoading()
        console.log(data)
        this.setData({
          freight: data.data.data,
          msk: true
        })
      }
    })
  },
  // 确认收货
  sure(e){
    wx.showModal({
      title: '提示',
      content: '确定要确认收货吗？',
      success: res => {
        if (res.confirm){
          wx.showLoading({
            title: '正在加载...',
            mask: true
          })
          let order_id = e.currentTarget.dataset.id,
            self = this,
            baseUrl = getApp().globalData.baseUrl,
            access_token = getApp().globalData.access_token,
            header = getApp().globalData.header
          wx.request({
            url: baseUrl + '/MyOrder/confirmSend',
            method: 'POST',
            header,
            data: { access_token, order_id },
            success: data => {
              if (data.data.code == 200) {
                wx.showToast({
                  title: '确认收货成功',
                })
                setTimeout(function () {
                  self.getData()
                }, 1000)
              }
            }
          })
        }
      }
    })
  },
  // 退/换货
  returnGoods(e) {
    wx.showLoading({
      title: '正在加载...',
    })
    let id = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.index,
      self = this,
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      freight_num = parseInt(e.currentTarget.dataset.freight_num)
    wx.request({
      url: baseUrl + '/MyOrder/order',
      data: { access_token, id, freight_num },
      method: 'POST',
      header,
      success: data => {
        wx.hideLoading()
        console.log(self.data.data)
        console.log(self.data.data.order_receive)
        console.log("售后服务状态：", self.data.data.order_receive[index].customer_service_status)
        // if (data.data.code == 200) {
        // }
        if (data.data.data.order_receive[index].service_status == 1) {//已经申请了退换货了
          if (data.data.data.order_receive[index].customer_service_status == 0) {//待审核
            wx.showToast({
              title: '待审核'
            })
          } else if (data.data.data.order_receive[index].customer_service_status == 1) {//审核通过
            if (data.data.data.order_receive[index].service_type == 2) {//换货
              wx.showToast({
                title: '审核通过,请到全部订单里查看您的换货状态'
              })
            } else {//退货
              wx.showToast({
                title: '审核通过,货款已原路退回'
              })
            }
          } else {
            wx.showToast({
              title: '审核未通过,请联系客服'
            })
          }
        } else {//跳转到退、换货页面

          wx.navigateTo({
            url: `../after-sales_service/after-sales_service?order_id=${id}`
          });
          this.setData({
            freight_num: data.data.data,
          })

        }       
      }
    })
  },
  hideMsk(e){
    console.log(e)
    if(e.target.dataset.name == 'msk'){
      this.setData({
        msk: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let active = options.active;
    let self = this;
    self.setData({
      active_: active
    })
    console.log("获取页签号：",active)
    this.computed_total('all')
    this.computed_total('wait_pay')
    if (!getApp().globalData.access_token){
      getApp().setUser(function(){
        return self.getData()
      })
    }else{
      self.getData()
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})